package com.tbit.main.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountConfigExt;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.AccountConfigExtService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.RequestContext;

import java.util.List;

/**
 * 区域配置
 *
 * @author LMY
 * @create 2023-04-03 15:42
 */
@Slf4j
@RestController
@RequestMapping("/AccountConfigExt")
public class AccountConfigExtController {

    @Autowired
    private AccountConfigExtService accountConfigExtService;

    /**
     * 修改
     *
     * @param token
     * @param accountConfigExt 区域参数
     * @return
     */
    @RequestMapping("/update")
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.ACCOUNT_CONFIG_UPDATE)
    public JsonResult update(String token, AccountConfigExt accountConfigExt) {
        AccountConfigExt accountConfigExtDB = accountConfigExtService.getParamKey(accountConfigExt.getAccountId(), accountConfigExt.getParamKey());
        if (accountConfigExtDB != null) {
            accountConfigExtService.update(accountConfigExt);
        } else {
            accountConfigExtService.insert(accountConfigExt);
        }
        return JsonResult.succ();
    }

    /**
     * 添加
     *
     * @param token
     * @param accountConfigExt 区域参数
     * @return
     */
    @RequestMapping("/insert")
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.ACCOUNT_CONFIG_ADD)
    public JsonResult insert(String token, AccountConfigExt accountConfigExt) {
        accountConfigExtService.insert(accountConfigExt);
        return JsonResult.succ();
    }

    /**
     * 查询
     *
     * @param token
     * @param accountConfigExt 区域参数
     * @return
     */
    @RequestMapping("/getParamKey")
    public JsonResult getParamKey(String token, AccountConfigExt accountConfigExt) {
        return JsonResult.succ(accountConfigExtService.getParamKey(accountConfigExt.getAccountId(), accountConfigExt.getParamKey()));
    }

    /**
     * @description:  查询 头盔锁配置拓展字段
     * @author: mo.shanyong
     * @param: accountId 区域id
     * @return: com.tbit.main.common.JsonResult
     */
    @RequestMapping("/getHelmetParam")
    public Result<List<AccountConfigExt>> getHelmetParam(Integer accountId) {
        List<AccountConfigExt> accountConfigExtList = accountConfigExtService.getHelmetParam(accountId);
        return Result.success(accountConfigExtList);
    }

    /**
     * 新增区域参数扩展配置
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.ADD_ACCOUNT_CONFIG_EXT)
    public Result<Object> add(@RequestBody String data) {
        log.info("新增区域参数扩展配置------>data:{}",data);
        List<AccountConfigExt> accountConfigExts = JSONArray.parseArray(data, AccountConfigExt.class);
        accountConfigExtService.insertAccountConfigExt(accountConfigExts);
        return Result.success();
    }
}
